package com.lx.huimailf;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lx.huimailf.activity.LoginActivity;
import com.lx.huimailf.activity.MainFragmentActivity;
import com.lx.huimailf.configs.Config;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.Call;

/**
 * 实现首次启动的引导页面
 */
public class SplashActivity extends AppCompatActivity {
	SharedPreferences preferences;
	private ImageView logo;
	private SharedPreferences sp;
	String isvalid,custtypeid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//获得实例对象
		sp =getSharedPreferences("userInfo", Context.MODE_PRIVATE);
		logo=(ImageView)findViewById(R.id.logo);

			new Handler().postDelayed(r, 1000);
		}



	Runnable r = new Runnable() {
		@Override
		public void run() {
			//判断记住密码多选框的状态
			if (sp.getBoolean("ISCHECK", false)) {
				//判断自动登陆多选框状态
				if (sp.getBoolean("AUTO_ISCHECK", false)) {
					//跳转界面
					HashMap<String,String> map=new HashMap<String, String>();
					map.put("account",sp.getString("USER_NAME", ""));
					map.put("password",sp.getString("PASSWORD", ""));
					Gson gson=new Gson();
					String parmas=gson.toJson(map).toString();
					OkHttpUtils
							.post()
							.url(Config.url+"login/appLogin.do")
							.addParams("data",parmas)
							.build()
							.execute(new StringCallback() {
								@Override
								public void onError(Call call, Exception e, int id) {
									Toast.makeText(SplashActivity.this,"登录失败",Toast.LENGTH_SHORT).show();
								}

								@Override
								public void onResponse(String response, int id) {
									System.out.print("登录成功"+response);
									if(response.length()<30){
										Intent intent = new Intent();
										intent.setClass(SplashActivity.this, LoginActivity.class);
										startActivity(intent);
										finish();
									}
									JSONArray jsonArray=null;
									JSONObject jsonObject=null;
									try {
										jsonArray=new JSONArray(response);
										jsonObject=jsonArray.getJSONObject(0);

										isvalid=jsonObject.getString("isvalid");
										custtypeid=jsonObject.getString("custtypeid");
										String custid=jsonObject.getString("id");
										SharedPreferences preferences=getSharedPreferences("user", Context.MODE_PRIVATE);
										SharedPreferences.Editor editor=preferences.edit();
										editor.putString("custid",custid);
										editor.putBoolean("status",true);
										editor.commit();
										isvalid=jsonObject.getString("isvalid");
										if (isvalid.equals("0")){
											Intent intent = new Intent();
											intent.setClass(SplashActivity.this, LoginActivity.class);
											startActivity(intent);
											finish();
										}
										Intent intent = new Intent(SplashActivity.this, MainFragmentActivity.class);
										startActivity(intent);
										finish();
									} catch (JSONException e) {
										e.printStackTrace();
									}

								}
							});

				}else {
					Intent intent = new Intent();
					intent.setClass(SplashActivity.this, LoginActivity.class);
					startActivity(intent);
					finish();
				}
			}else {
				Intent intent = new Intent();
				intent.setClass(SplashActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}


		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}

